cmake_minimum_required(VERSION 3.5)

project(sim_client)

add_subdirectory(third_party)

set(CMAKE_CXX_STANDARD 14)

corrosion_import_crate(MANIFEST_PATH Cargo.toml)

# TODO:
set (CXX_BRIDGE_BINARY_DIR
    ${CMAKE_CURRENT_BINARY_DIR}/cargo/build/${Rust_CARGO_TARGET_CACHED}/cxxbridge/sim-client/src)
set (CXX_BRIDGE_HDR1 ${CMAKE_CURRENT_SOURCE_DIR}/include/simulation/client/detail/ffi.rs.h)
set (CXX_BRIDGE_HDR2 ${CMAKE_CURRENT_SOURCE_DIR}/include/simulation/client/detail/cxx.h)
set (CXX_BRIDGE_SRC ${CXX_BRIDGE_BINARY_DIR}/ffi.rs.cc)
add_custom_command(
    OUTPUT ${CXX_BRIDGE_HDR1} ${CXX_BRIDGE_HDR2} ${CXX_BRIDGE_SRC}
    COMMAND cp ${CXX_BRIDGE_BINARY_DIR}/ffi.rs.h ${CXX_BRIDGE_HDR1}
    COMMAND cp ${CXX_BRIDGE_BINARY_DIR}/../../rust/cxx.h ${CXX_BRIDGE_HDR2}
    DEPENDS sim-client
)

set(MIDDLE_LIB_NAME ${PROJECT_NAME}_middle)
add_library(${MIDDLE_LIB_NAME} ${CXX_BRIDGE_SRC})
set_target_properties(${MIDDLE_LIB_NAME} PROPERTIES POSITION_INDEPENDENT_CODE True)
target_include_directories(${MIDDLE_LIB_NAME} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(${MIDDLE_LIB_NAME} sim-client openssl)

set(FINAL_LIB_NAME ${PROJECT_NAME}_cc)
add_library(${FINAL_LIB_NAME} src/client.cc)
set_target_properties(${FINAL_LIB_NAME} PROPERTIES POSITION_INDEPENDENT_CODE True)
target_include_directories(${FINAL_LIB_NAME} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(${FINAL_LIB_NAME} ${MIDDLE_LIB_NAME})

set(TEST_NAME ${PROJECT_NAME}_test)
add_executable(${TEST_NAME} src/test.cc)
target_link_libraries(${TEST_NAME} ${FINAL_LIB_NAME})
